package com.sockrt;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/** SocketClint01
 * @author 陈志安
 * @version 1.0
 * 客户端
 */
public class SocketClint01 {
    public static void main(String[] args) throws IOException {
        //思路
        //1.连接服务端(ip,端口)
        Socket socket=new Socket(InetAddress.getLocalHost(),9999);
        //2.连接上后,生成socket
        System.out.println("客户端"+socket.getClass());
        OutputStream outputStream=socket.getOutputStream();
        outputStream.write("hello my lover".getBytes());
        socket.shutdownOutput();

        //读消息
        InputStream inputStream=socket.getInputStream();
        int readline=0;
        byte[]buf=new byte[100];
        while((readline=inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,readline));
        }

        outputStream.close();
        socket.close();
    }
}
